package com.example.shopp_2301.update

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shopp_2301.R
import com.example.shopp_2301.databinding.ActivityUpdatePwdBinding

@Route(path = "/app/UpdatePwdActivity")
class UpdatePwdActivity : BaseActivity<ActivityUpdatePwdBinding,UpdateViewModel>() {
    override fun initView() {
        //显示手机号
        val phone = SPUtils.getInstance().getString("phone")
        val phoneStr = phone.substring(0,3)+"****"+phone.substring(7,11)
        binding.tvPhone.text = "当前手机号：$phoneStr"

        binding.sendCode.setOnClickListener {
            viewModel.sendCode(phone)
        }
        binding.update.setOnClickListener {
            val pwd = binding.etPwd.text.toString()
            val repwd = binding.etRepwd.text.toString()
            if(pwd != repwd){
                ToastUtils.showLong("两次输入的密码不一致")
                return@setOnClickListener
            }
            viewModel.updatePwd(phone,binding.etCode.text.toString(),pwd)
        }
    }

    override fun initData() {
        viewModel.sendCodeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
        }
        viewModel.sendCodeFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.updatePwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
                ARouter.getInstance().build("/app/LoginActivity").navigation()
            }
        }
        viewModel.updatePwdFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<UpdateViewModel> {
        return UpdateViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_update_pwd
    }

}